{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from easyquant import MongoIo\n",
    "from easyquant.indicator.base import *\n",
    "import easyquotation\n",
    "import math\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from easyquant import MongoIo\n",
    "# from easyquant.qafetch import QATdx as tdx\n",
    "from easyquant.indicator.base import *\n",
    "import pandas as pd\n",
    "from easyquant import EasyTime\n",
    "from datetime import datetime, time\n",
    "from ctypes import * \n",
    "# import datetime\n",
    "import math\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import talib\n",
    "from ctypes import * \n",
    "import QUANTAXIS as QA\n",
    "\n",
    "# import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "m=MongoIo()\n",
    "source=easyquotation.use('sina')\n",
    "table = 'st_orders-%s' % 'tdx_yhzc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def new_df(df_day, data, now_price):\n",
    "    code = data['code']\n",
    "    # now_vol = data['volume']\n",
    "    last_time = pd.to_datetime(data['datetime'][0:10])\n",
    "    # print(\"code=%s, data=%s\" % (self.code, self._data['datetime']))\n",
    "    # df_day = data_buf_day[code]\n",
    "    # df_day.loc[last_time]=[0 for x in range(len(df_day.columns))]\n",
    "    df_day.at[(last_time, code), 'open'] = data['open']\n",
    "    df_day.at[(last_time, code), 'high'] = data['high']\n",
    "    df_day.at[(last_time, code), 'low'] = data['low']\n",
    "    df_day.at[(last_time, code), 'close'] = now_price\n",
    "    df_day.at[(last_time, code), 'volume'] = data['volume']\n",
    "    df_day.at[(last_time, code), 'amount'] = data['amount']\n",
    "    return df_day\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tdx_dqe_cfc_A1(data, sort=False):\n",
    "    # 选择／排序\n",
    "    C = data.close\n",
    "    O = data.open\n",
    "    JC =IF(ISLASTBAR(C), O, C)\n",
    "    MC = (0.3609454219 * JC - 0.03309329629 * REF(C, 1) - 0.04241822779 * REF(C, 2) - 0.026737249 * REF(C, 3) \\\n",
    "           - 0.007010041271 * REF(C, 4) - 0.002652859952 * REF(C, 5) - 0.0008415042966 * REF(C, 6) \\\n",
    "           - 0.0002891931964 * REF(C, 7) - 0.0000956265934 * REF(C, 8) - 0.0000321286052 * REF(C, 9) \\\n",
    "           - 0.0000106773454 * REF(C, 10) - 0.0000035457562 * REF(C, 11) -- 0.0000011670713 * REF(C, 12)) / (1 - 0.7522406533)\n",
    "    # 竞价涨幅 := (DYNAINFO(4) / DYNAINFO(3) - 1) * 100;\n",
    "    竞价涨幅 = (C / REF(C, 1) - 1) * 100\n",
    "    # ST := STRFIND(stkname, 'ST', 1) > 0;\n",
    "    # S := STRFIND(stkname, 'S', 1) > 0;\n",
    "    # 停牌 := (DYNAINFO(4)=0);\n",
    "    #\n",
    "    # 附加条件 := (not (ST) and not (S) and NOT(停牌)) * (竞价涨幅 < 9.85) * (竞价涨幅 > (0));\n",
    "    附加条件 = IFAND(竞价涨幅 < 9.85, 竞价涨幅 > 0, 1, 0)\n",
    "#     if sort:\n",
    "    刀 = (MC - JC) / JC * 1000\n",
    "#     else:\n",
    "#         刀 = (MC - JC) / JC * 1000 * 附加条件\n",
    "\n",
    "    return 刀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tdx_dqe_cfc_A12(data, sort=False):\n",
    "    # 选择／排序\n",
    "    C = data.close\n",
    "    O = data.open\n",
    "    JC = IF(ISLASTBAR(C), O, C)\n",
    "    MC = (0.3609454219 * JC - 0.03309329629 * REF(C, 1) - 0.04241822779 * REF(C, 2) - 0.026737249 * REF(C, 3) \\\n",
    "           - 0.007010041271 * REF(C, 4) - 0.002652859952 * REF(C, 5) - 0.0008415042966 * REF(C, 6) \\\n",
    "           - 0.0002891931964 * REF(C, 7) - 0.0000956265934 * REF(C, 8) - 0.0000321286052 * REF(C, 9) \\\n",
    "           - 0.0000106773454 * REF(C, 10) - 0.0000035457562 * REF(C, 11) -- 0.0000011670713 * REF(C, 12)) / (1 - 0.7522406533)\n",
    "    # 竞价涨幅 := (DYNAINFO(4) / DYNAINFO(3) - 1) * 100;\n",
    "    竞价涨幅 = (O / REF(C, 1) - 1) * 100\n",
    "    # ST := STRFIND(stkname, 'ST', 1) > 0;\n",
    "    # S := STRFIND(stkname, 'S', 1) > 0;\n",
    "    # 停牌 := (DYNAINFO(4)=0);\n",
    "    #\n",
    "    # 附加条件 := (not (ST) and not (S) and NOT(停牌)) * (竞价涨幅 < 9.85) * (竞价涨幅 > (0));\n",
    "    附加条件 = IFAND(竞价涨幅 < 9.85, 竞价涨幅 > 0, 1, 0)\n",
    "#     if sort:\n",
    "    刀 = (MC - JC) / JC * 1000\n",
    "#     else:\n",
    "#         刀 = (MC - JC) / JC * 1000 * 附加条件\n",
    "\n",
    "    return 刀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def tdx_yhzc(data):\n",
    "    # 用户注册\n",
    "    # pass\n",
    "    # C = data.close\n",
    "    CLOSE = data.close\n",
    "    # HIGH = data.high\n",
    "    # H = data.high\n",
    "    # L = data.low\n",
    "    # LOW = data.low\n",
    "    # OPEN = data.open\n",
    "    # O = data.open\n",
    "    VOL = data.volume\n",
    "    # AMOUNT = data.amount\n",
    "\n",
    "    # 除业绩后退股 := FINANCE(30) >= REF(FINANCE(30), 130);\n",
    "    # D0 := 除业绩后退股;\n",
    "    # D2 := IF(NAMELIKE('S'), 0, 1);\n",
    "    # D3 := IF(NAMELIKE('*'), 0, 1);\n",
    "    # D4 := DYNAINFO(17) > 0;\n",
    "    # 去除大盘股 := CAPITAL / 1000000 < 20;\n",
    "    # 去高价 := C <= 60;\n",
    "    # 去掉 := D0 and D2 and D3 and D4 and 去除大盘股 and 去高价 and NOT(C >= REF(C, 1) * 1.097 and C = O and H = L);\n",
    "    TJ_V = VOL > 3 * MA(VOL,89)\n",
    "    DIF1 = (EMA(CLOSE, 12) - EMA(CLOSE, 26)) / EMA(CLOSE, 26) * 100\n",
    "    DEA1 = EMA(DIF1, 9)\n",
    "    AAA1 = (DIF1 - DEA1) * 100\n",
    "    # MA120 = REF(MA(C,120),1)\n",
    "    # MA5 = REF(MA(C, 120),1)\n",
    "    # MA10 = REF(MA(C, 120),1)\n",
    "    # PTGD = REF(HHV(C,120),1)\n",
    "    # XIN_GAO = IFAND(C > PTGD, C > MA120, True, False)\n",
    "    用 = 45\n",
    "    户 = AAA1 - REF(AAA1, 1)\n",
    "    注册 = CROSS(户, 用)\n",
    "    DIF = (EMA(CLOSE, 10) - EMA(CLOSE, 72)) / EMA(CLOSE, 72) * 100\n",
    "    DEA = EMA(DIF, 17)\n",
    "    AAA = (DIF - DEA) * 100\n",
    "    用户 = CROSS(AAA - REF(AAA, 1), 45)\n",
    "    # 用户注册 = IFAND4(注册 , 用户, TJ_V, XIN_GAO, 1, 0) #and 去掉;\n",
    "    用户注册 = IFAND3(注册 , 用户, TJ_V, 1, 0) #and 去掉;\n",
    "    return 用户注册\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "# today\n",
    "code='300787'\n",
    "data=m.get_stock_day(code)\n",
    "newdata=source.stocks(code)[code]\n",
    "now_price = newdata['open']\n",
    "data = new_df(data.copy(), newdata, now_price)\n",
    "# data.iat[-1,3]=newdata['open']\n",
    "data.tail()\n",
    "C = data.close\n",
    "O = data.open\n",
    "V = data.volume\n",
    "H = data.high\n",
    "L = data.low\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>A1</th>\n",
       "      <th>yhzc</th>\n",
       "      <th>tj1</th>\n",
       "      <th>tj2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-11-03</th>\n",
       "      <th>300787</th>\n",
       "      <td>46.34</td>\n",
       "      <td>47.58</td>\n",
       "      <td>45.66</td>\n",
       "      <td>46.73</td>\n",
       "      <td>2.683100e+04</td>\n",
       "      <td>125587848.0</td>\n",
       "      <td>-4.134629</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-04</th>\n",
       "      <th>300787</th>\n",
       "      <td>46.88</td>\n",
       "      <td>47.67</td>\n",
       "      <td>46.29</td>\n",
       "      <td>47.31</td>\n",
       "      <td>3.035000e+04</td>\n",
       "      <td>142469968.0</td>\n",
       "      <td>6.291537</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-05</th>\n",
       "      <th>300787</th>\n",
       "      <td>48.36</td>\n",
       "      <td>49.15</td>\n",
       "      <td>47.50</td>\n",
       "      <td>48.48</td>\n",
       "      <td>3.927800e+04</td>\n",
       "      <td>190026400.0</td>\n",
       "      <td>15.684385</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-06</th>\n",
       "      <th>300787</th>\n",
       "      <td>48.39</td>\n",
       "      <td>48.65</td>\n",
       "      <td>46.59</td>\n",
       "      <td>46.87</td>\n",
       "      <td>3.400800e+04</td>\n",
       "      <td>161310304.0</td>\n",
       "      <td>-5.548653</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-09</th>\n",
       "      <th>300787</th>\n",
       "      <td>47.80</td>\n",
       "      <td>50.88</td>\n",
       "      <td>47.80</td>\n",
       "      <td>47.80</td>\n",
       "      <td>2.156544e+08</td>\n",
       "      <td>4322145.0</td>\n",
       "      <td>2.342464</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   high    low  close        volume       amount  \\\n",
       "date       code                                                            \n",
       "2020-11-03 300787  46.34  47.58  45.66  46.73  2.683100e+04  125587848.0   \n",
       "2020-11-04 300787  46.88  47.67  46.29  47.31  3.035000e+04  142469968.0   \n",
       "2020-11-05 300787  48.36  49.15  47.50  48.48  3.927800e+04  190026400.0   \n",
       "2020-11-06 300787  48.39  48.65  46.59  46.87  3.400800e+04  161310304.0   \n",
       "2020-11-09 300787  47.80  50.88  47.80  47.80  2.156544e+08    4322145.0   \n",
       "\n",
       "                          A1  yhzc    tj1   tj2  \n",
       "date       code                                  \n",
       "2020-11-03 300787  -4.134629     0  False  True  \n",
       "2020-11-04 300787   6.291537     0  False  True  \n",
       "2020-11-05 300787  15.684385     0   True  True  \n",
       "2020-11-06 300787  -5.548653     0   True  True  \n",
       "2020-11-09 300787   2.342464     0   True  True  "
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TJ1=IFAND(O > REF(MA(C, 5), 1),  O > REF(MA(C, 10), 1), True, False)\n",
    "TJ2=(COUNT(REF(V, 1) / REF(V, 2) > 6, 10) == 0)\n",
    "ccc=tdx_dqe_cfc_A1(data)\n",
    "yhzc=tdx_yhzc(data)\n",
    "data['A1']=ccc\n",
    "data['yhzc']=yhzc\n",
    "data['tj1']=IFAND(O > REF(MA(C, 5), 1),  O > REF(MA(C, 10), 1), True, False)\n",
    "data['tj2']=(COUNT(REF(V, 1) / REF(V, 2) > 6, 10) == 0)\n",
    "# result\n",
    "data.tail()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "# back:\n",
    "code='002242'\n",
    "data=m.get_stock_day(code, st_end='2020-11-05')\n",
    "\n",
    "C = data.close\n",
    "O = data.open\n",
    "V = data.volume\n",
    "H = data.high\n",
    "L = data.low\n",
    "# data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>A1</th>\n",
       "      <th>tj1</th>\n",
       "      <th>tj2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-10-30</th>\n",
       "      <th>002242</th>\n",
       "      <td>36.65</td>\n",
       "      <td>36.70</td>\n",
       "      <td>34.81</td>\n",
       "      <td>35.36</td>\n",
       "      <td>81318.0</td>\n",
       "      <td>290368544.0</td>\n",
       "      <td>-40.651927</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-02</th>\n",
       "      <th>002242</th>\n",
       "      <td>35.10</td>\n",
       "      <td>36.29</td>\n",
       "      <td>35.10</td>\n",
       "      <td>35.83</td>\n",
       "      <td>58381.0</td>\n",
       "      <td>208967888.0</td>\n",
       "      <td>-14.172844</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-03</th>\n",
       "      <th>002242</th>\n",
       "      <td>35.83</td>\n",
       "      <td>36.74</td>\n",
       "      <td>35.19</td>\n",
       "      <td>36.32</td>\n",
       "      <td>67585.0</td>\n",
       "      <td>243814624.0</td>\n",
       "      <td>2.036720</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-04</th>\n",
       "      <th>002242</th>\n",
       "      <td>36.18</td>\n",
       "      <td>36.94</td>\n",
       "      <td>36.00</td>\n",
       "      <td>36.82</td>\n",
       "      <td>57094.0</td>\n",
       "      <td>208352560.0</td>\n",
       "      <td>9.963941</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-11-05</th>\n",
       "      <th>002242</th>\n",
       "      <td>37.03</td>\n",
       "      <td>37.70</td>\n",
       "      <td>36.55</td>\n",
       "      <td>37.00</td>\n",
       "      <td>51057.0</td>\n",
       "      <td>189177312.0</td>\n",
       "      <td>8.671438</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   high    low  close   volume       amount  \\\n",
       "date       code                                                       \n",
       "2020-10-30 002242  36.65  36.70  34.81  35.36  81318.0  290368544.0   \n",
       "2020-11-02 002242  35.10  36.29  35.10  35.83  58381.0  208967888.0   \n",
       "2020-11-03 002242  35.83  36.74  35.19  36.32  67585.0  243814624.0   \n",
       "2020-11-04 002242  36.18  36.94  36.00  36.82  57094.0  208352560.0   \n",
       "2020-11-05 002242  37.03  37.70  36.55  37.00  51057.0  189177312.0   \n",
       "\n",
       "                          A1    tj1   tj2  \n",
       "date       code                            \n",
       "2020-10-30 002242 -40.651927  False  True  \n",
       "2020-11-02 002242 -14.172844  False  True  \n",
       "2020-11-03 002242   2.036720  False  True  \n",
       "2020-11-04 002242   9.963941  False  True  \n",
       "2020-11-05 002242   8.671438  False  True  "
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=data.copy()\n",
    "ccc=tdx_dqe_cfc_A12(data).tail()\n",
    "data['A1']=ccc\n",
    "data['tj1']=IFAND(O > REF(MA(C, 5), 1),  O > REF(MA(C, 10), 1), True, False)\n",
    "data['tj2']=(COUNT(REF(V, 1) / REF(V, 2) > 6, 10) == 0)\n",
    "# result\n",
    "data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-112066005.859375"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code='300612'\n",
    "dataInfo = m.get_stock_info(code)\n",
    "dataInfo.jinglirun[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-211-d05762d70d5f>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-211-d05762d70d5f>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    300364\t7.62\t8.49\t7.38\t7.92\t787380.0\t621797440.0\t48.325356\u001b[0m\n\u001b[0m          \t   ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "300364\t7.62\t8.49\t7.38\t7.92\t787380.0\t621797440.0\t48.325356\n",
    "300464\t20.50\t22.88\t20.08\t22.30\t249674.0\t533748384.0\t0.000000\tTrue\tTrue\n",
    "300689\t48.51\t53.56\t47.11\t49.70\t51398.0\t258483824.0\t25.845675"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataInfo = mongo.get_stock_info(code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qawork",
   "language": "python",
   "name": "qawork"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
